package com.zzyl.nursing.service.impl;

import java.util.Arrays;
import java.util.List;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzyl.common.constant.CacheConstants;
import com.zzyl.common.constant.HttpStatus;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.common.utils.StringUtils;
import com.zzyl.nursing.dto.NursingProjectDto;
import com.zzyl.nursing.vo.NursingProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.NursingProjectMapper;
import com.zzyl.nursing.domain.NursingProject;
import com.zzyl.nursing.service.INursingProjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 护理项目Service业务层处理
 * 
 * @author alexis
 * @date 2024-12-30
 */
@Service
public class NursingProjectServiceImpl extends ServiceImpl<NursingProjectMapper, NursingProject> implements INursingProjectService
{
    @Autowired
    private NursingProjectMapper nursingProjectMapper;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    /**
     * 查询护理项目
     * 
     * @param id 护理项目主键
     * @return 护理项目
     */
    @Override
    public NursingProject selectNursingProjectById(Long id)
    {
        return nursingProjectMapper.selectById(id);
    }

    /**
     * 查询护理项目列表
     * 
     * @param nursingProject 护理项目
     * @return 护理项目
     */
    @Override
    public List<NursingProject> selectNursingProjectList(NursingProject nursingProject)
    {
        return nursingProjectMapper.selectNursingProjectList(nursingProject);
    }

    /**
     * 新增护理项目
     * 
     * @param nursingProject 护理项目
     * @return 结果
     */
    @Override
    public int insertNursingProject(NursingProject nursingProject)
    {
        int insert = nursingProjectMapper.insert(nursingProject);
        redisTemplate.delete(CacheConstants.NURSING_PROJECT_ALL_KEY);
        return insert;
    }

    /**
     * 修改护理项目
     * 
     * @param nursingProject 护理项目
     * @return 结果
     */
    @Override
    public int updateNursingProject(NursingProject nursingProject)
    {
        int update = nursingProjectMapper.updateById(nursingProject);
        redisTemplate.delete(CacheConstants.NURSING_PROJECT_ALL_KEY);
        return update;
    }

    /**
     * 批量删除护理项目
     * 
     * @param ids 需要删除的护理项目主键
     * @return 结果
     */
    @Override
    public int deleteNursingProjectByIds(Long[] ids)
    {
        int deleteBatchIds = nursingProjectMapper.deleteBatchIds(Arrays.asList(ids));
        redisTemplate.delete(CacheConstants.NURSING_PROJECT_ALL_KEY);
        return deleteBatchIds;
    }

    /**
     * 删除护理项目信息
     * 
     * @param id 护理项目主键
     * @return 结果
     */
    @Override
    public int deleteNursingProjectById(Long id)
    {
        int deleteById = nursingProjectMapper.deleteById(id);
        redisTemplate.delete(CacheConstants.NURSING_PROJECT_ALL_KEY);
        return deleteById;
    }

    /**
     * 查询所有护理项目VO列表
     *
     * @return 结果
     */
    @Override
    public List<NursingProjectVo> getAllProjects() {
        // 1.在缓存中查询
        String result = redisTemplate.opsForValue().get(CacheConstants.NURSING_PROJECT_ALL_KEY);

        // 2.如果查到，将JSON字符串转换为集合再返回
        if (StringUtils.isNotEmpty(result)){
            return JSON.parseArray(result,NursingProjectVo.class);
        }

        // 3.如果没有查到，查询数据库
        List<NursingProjectVo> allProjects = nursingProjectMapper.getAllProjects();

        // 4.将查询结果存入缓存
        redisTemplate.opsForValue().set(CacheConstants.NURSING_PROJECT_ALL_KEY,JSON.toJSONString(allProjects));

        // 5.返回查询结果
        return allProjects;
    }

    /**
     * 分页条件查询项目列表
     *
     * @param nursingProjectDto
     * @return
     */
    @Override
    public TableDataInfo<NursingProject> pageQuery(NursingProjectDto nursingProjectDto) {
        Page<NursingProject> page = new Page<>(nursingProjectDto.getPageNum(), nursingProjectDto.getPageSize());
        LambdaQueryWrapper<NursingProject> queryWrapper = Wrappers.<NursingProject>lambdaQuery().eq(nursingProjectDto.getStatus() != null, NursingProject::getStatus, nursingProjectDto.getStatus())
                .like(StringUtils.isNotEmpty(nursingProjectDto.getName()), NursingProject::getName, nursingProjectDto.getName());
        page = page(page, queryWrapper);

        return getTableDataInfo(page);
    }

    private TableDataInfo<NursingProject> getTableDataInfo(Page<NursingProject> page) {
        TableDataInfo<NursingProject> tableDataInfo = new TableDataInfo<>();
        tableDataInfo.setTotal(page.getTotal());
        tableDataInfo.setRows(page.getRecords());
        tableDataInfo.setCode(HttpStatus.SUCCESS);
        tableDataInfo.setMsg("查询成功");
        return tableDataInfo;
    }
}
